package com.njwb.Swing.Users;

import com.njwb.Util.UtilService;

import javax.swing.*;
import java.awt.*;

public class AddUserDialog extends JDialog {
    private JTextField usernameField;
    private JPasswordField passwordField, confirmPasswordField;
    private JTextField genderField, ageField, phoneField, addressField;
    private JComboBox<String> roleBox;
    private JButton confirmButton, cancelButton;
    private UtilService utilService;
    private Runnable refreshCallback;

    public AddUserDialog(Frame owner, UtilService utilService, Runnable refreshCallback) {
        super(owner, "新增用户", true);
        this.utilService = utilService;
        this.refreshCallback = refreshCallback;

        setSize(400, 380);
        setLocationRelativeTo(owner);
        setLayout(new GridLayout(9, 2, 5, 5)); // 行数增加一行

        add(new JLabel("用户名:")); usernameField = new JTextField(); add(usernameField);
        add(new JLabel("密码:")); passwordField = new JPasswordField(); add(passwordField);
        add(new JLabel("确认密码:")); confirmPasswordField = new JPasswordField(); add(confirmPasswordField); // 新增
        add(new JLabel("性别:")); genderField = new JTextField(); add(genderField);
        add(new JLabel("年龄:")); ageField = new JTextField(); add(ageField);
        add(new JLabel("电话:")); phoneField = new JTextField(); add(phoneField);
        add(new JLabel("地址:")); addressField = new JTextField(); add(addressField);
        add(new JLabel("角色:")); roleBox = new JComboBox<>(new String[]{"普通用户", "管理员"}); add(roleBox);

        confirmButton = new JButton("确认"); cancelButton = new JButton("取消");
        add(confirmButton); add(cancelButton);

        confirmButton.addActionListener(e -> {
            try {
                String username = usernameField.getText().trim();
                String password = new String(passwordField.getPassword());
                String confirmPassword = new String(confirmPasswordField.getPassword()); // 获取确认密码
                if (!password.equals(confirmPassword)) {
                    JOptionPane.showMessageDialog(this, "两次输入的密码不一致！", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                String gender = genderField.getText().trim();
                int age = Integer.parseInt(ageField.getText().trim());
                String phone = phoneField.getText().trim();
                String address = addressField.getText().trim();
                int role = roleBox.getSelectedIndex();

                boolean success = utilService.addUser(username, password, gender, age, phone, address, role);
                if (success) {
                    JOptionPane.showMessageDialog(this, "新增成功！");
                    dispose();
                    if (refreshCallback != null) refreshCallback.run();
                } else {
                    JOptionPane.showMessageDialog(this, "新增失败！", "错误", JOptionPane.ERROR_MESSAGE);
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, "输入数据有误！", "错误", JOptionPane.ERROR_MESSAGE);
            }
        });

        cancelButton.addActionListener(e -> dispose());
    }
}
